#if !defined(___JPEG1_C)
#define ___JPEG1_C
#include "std.h"


#define FLGDC       0x1000000ul


static N2 tab1[64]=
{  3, 2, 2, 3, 2, 2, 3, 3,
   3, 3, 4, 3, 3, 4, 5, 8,
   5, 5, 4, 4, 5,10, 7, 7,
   6, 8,12,10,12,12,11,10,
  11,11,13,14,18,16,13,14,
  17,14,11,11,16,22,16,17,
  19,20,21,21,21,12,15,23,
  24,22,20,24,18,20,21,20,
};
static N2 tab2[64]=
{  3, 4, 4, 5, 4, 5, 9, 5,
   5, 9,20,13,11,13,20,20,
  20,20,20,20,20,20,20,20,
  20,20,20,20,20,20,20,20,
  20,20,20,20,20,20,20,20,
  20,20,20,20,20,20,20,20,
  20,20,20,20,20,20,20,20,
  20,20,20,20,20,20,20,20,
};


static xx fwrite1(N1 d1,FILE *f1)
{ fwrite(&d1,1,1,f1);
}
static xx fwrite2(N2 d1,FILE *f1)
{ N1 b1=d1>>8;
  fwrite(&b1,1,1,f1);
  b1=d1&0xFF;
  fwrite(&b1,1,1,f1);
}


static Str help1=
  "Args: -h| [-v(0-4)] [-i<file_bmp>] [-o<file_out>] [-b<file_jpg>]\n"
  "Defs: -v:4, -i:<stdin>, -o:<stdout>, -b:<none>";
I4 opts1(tProg *prg,int argc,char **argv)
{ FILE *f1; N4 i;
  if (!prg) ERR1("Error: opts: NULL prg",0ul);
  prg->msg=4;
  prg->fi=stdin;
  prg->fo=stdout;
  prg->fb=NULL;

  if (!argc && !argv)
  { puts(help1); return (0); }
  for (++argv,i=1;i<argc;++i)
  { Str s1=*(argv++);
    if (s1[0]!='-') continue;
    switch (s1[1])
    { case 'h':
        puts(help1);
        exit(0);
      case 'v':
        prg->msg=s1[2]-'0';
        break;
      case 'i':
        if (!(f1=fopen(s1+2,"rb")))
          ERR1("Error: Open Input",0l);
        prg->fi=f1;
        break;
      case 'o':
        if (!(f1=fopen(s1+2,"wb")))
          ERR1("Error: Open Output",0l);
        prg->fo=f1;
        break;
      case 'b':
        if (!(f1=fopen(s1+2,"wb")))
          ERR1("Error: Open Bitmap",0l);
        prg->fb=f1;
        break;
    }
  }
  return (0);
}
xx jpeg1Beg(tJpeg1 *jpg)
{ N4 i1;
  jpg->bmp=NULL;
  for (i1=0;i1<CNUM;++i1)
  { jpg->pp[i1]=NULL;
    jpg->buf[i1]=NULL;
    jpg->buf1[i1]=NULL;
    jpg->buf2[i1]=NULL;
  }
  for (i1=0;i1<HMAX;++i1)
  { jpg->htab[i1].val=NULL;
    jpg->htab[i1].hufsiz=NULL;
    jpg->htab[i1].hufcod=NULL;
  }
}
xx jpeg1End(tJpeg1 *jpg)
{ N4 i1;
  if (jpg->bmp)
    MemRel(jpg->bmp->pp);
  for (i1=0;i1<CNUM;++i1)
  { MemRel(jpg->pp[i1]);
    MemRel(jpg->buf[i1]);
    MemRel(jpg->buf1[i1]);
    MemRel(jpg->buf2[i1]);
  }
  for (i1=0;i1<HMAX;++i1)
  { MemRel(jpg->htab[i1].val);
    MemRel(jpg->htab[i1].hufsiz);
    MemRel(jpg->htab[i1].hufcod);
  }
}
int jpeg1a(int argc,char **argv)
{ I4 i1;
  tJpeg1 jpg=
  { {2,1,1},
    {2,1,1},
    tab1,tab2,
  };
  jpeg1Beg(&jpg);
  if (!(i1=opts1(prg,argc,argv)))
    i1=jpeg1b(&jpg,prg->fi,prg->fb);
  jpeg1End(&jpg);
  if (prg->fi!=stdin) fclose(prg->fi);
  if (prg->fo!=stdout) fclose(prg->fo);
  if (prg->fb) fclose(prg->fb);
  if (i1==-1l) return (-1);
  return (0);
}
I4 jpeg1b(tJpeg1 *jpg,FILE *fi,FILE *fb)
{ DctInit();

  if (conv1a(jpg,fi)==-1l)
    return (-1l);
  if (head1(jpg,fb)==-1l)
    return (-1l);
  if (conv1b(jpg,fb)==-1l)
    return (-1l);
  // EOI
  fwrite2(0xFFD9u,fb);

  return (0);
}

I4 head1(tJpeg1 *jpg,FILE *f1)
{ N4 i1,i2,i3; N1 d1;
  N2 *q1; N1 *h1;

  // SOI
  fwrite2(0xFFD8u,f1);
  // APP0
  fwrite2(0xFFE0u,f1);
  //   Length
  fwrite2(16,f1);
  //   Identifier
  fwrite("JFIF",5,1,f1);
  //   Version
  fwrite2(0x0102u,f1);
  //   Units
  fwrite1(0,f1);
  //   X Density
  fwrite2(1,f1);
  //   Y Density
  fwrite2(1,f1);
  //   X Thumbnail
  fwrite1(0,f1);
  //   Y Thumbnail
  fwrite1(0,f1);
  // DQT
  for (i1=0;i1<2;++i1)
  { fwrite2(0xFFDBu,f1);
    // Length
    fwrite2(67,f1);
    // Precision,QID
    d1=(N1)i1;
    fwrite1(d1,f1);
    // Table
    q1=((i1==0)?
      jpg->tab1:jpg->tab2);
    for (i2=0;i2<64;++i2)
      fwrite1((N1)*(q1++),f1);
  }
  // SOF
  fwrite2(0xFFC0u,f1);
  //   Length
  fwrite2(17,f1);
  //   Precision
  fwrite1(8,f1);
  //   Y Length
  fwrite2(jpg->yl,f1);
  //   X Length
  fwrite2(jpg->xl,f1);
  //   Comp Num
  fwrite1(CNUM,f1);
  //   Components
  for (i1=0;i1<CNUM;++i1)
  { // CID
    fwrite1((N1)i1,f1);
    // Xd,Yd
    d1=(jpg->xd[i1]<<4)|jpg->yd[i1];
    fwrite1(d1,f1);
    // QID
    d1=((i1==0)?0:1);
    fwrite1(d1,f1);
  }
  // DHT
  for (i1=0;i1<HMAX;++i1)
  { fwrite2(0xFFC4u,f1);
    // Length
    i3=jpg->htab[i1].hufnum;
    fwrite2((N2)19+i3,f1);
    // Typ,HID
    d1=jpg->htab[i1].typ<<4;
    d1|=jpg->htab[i1].hid;
    fwrite1(d1,f1);
    // Lens
    h1=jpg->htab[i1].len;
    for (i2=0;i2<16;++i2)
      fwrite1(*(h1++),f1);
    // Vals
    h1=jpg->htab[i1].val;
    for (i2=0;i2<i3;++i2)
      fwrite1(*(h1++),f1);
  }
  // SOS
  fwrite2(0xFFDAu,f1);
  //   Length
  fwrite2(12,f1);
  //   Comp Num
  fwrite1(CNUM,f1);
  //   Components
  for (i1=0;i1<CNUM;++i1)
  { // CID
    fwrite1((N1)i1,f1);
    // HDC,HAC
    d1=((i1==0)?0x00u:0x11u);
    fwrite1(d1,f1);
  }
  //   Start Qt
  fwrite1(0,f1);
  //   Stop Qt
  fwrite1(63,f1);
  //   Approx H,L
  fwrite1(0,f1);

  return (0);
}

I4 conv1a(tJpeg1 *jpg,FILE *f1)
{ tBmp bmp; Str s1,s2,s3; N4 i1,i2,i3;
  N4 *buf[CNUM],*buf2[CNUM],*b1,*b2;
  N2 xm[CNUM],ym[CNUM],xl,yl;
  N1 *pp[CNUM],xd,yd,xd2,yd2;
  N4 len[CNUM],*buf3[CNUM];
  N2 x,y,x2,y2; I2 x1,y1;
  I2 dd[64],*p1;
  RI rr1[64],rr2[64];
  tItem itm[HMAX][256+1];
  N1 huflen[HMAX][32];
  N1 *hufval[HMAX];

  // Initializing
  for (xd=0,i1=0;i1<CNUM;++i1)
    if (jpg->xd[i1]>xd) xd=jpg->xd[i1];
  for (yd=0,i1=0;i1<CNUM;++i1)
    if (jpg->yd[i1]>yd) yd=jpg->yd[i1];
  bmp.pp=NULL; jpg->bmp=&bmp;
  if (BmpRead1(&bmp,f1,xd*8,yd*8)==-1l)
    return (-1l);
  for (i1=0;i1<CNUM;++i1)
  { if (i1==0) INF1("[........] XDens: (%u",
      jpg->xd[i1]); else INF1(",%u",jpg->xd[i1]);
  } INF0(")\n");
  for (i1=0;i1<CNUM;++i1)
  { if (i1==0) INF1("[........] YDens: (%u",
      jpg->yd[i1]); else INF1(",%u",jpg->yd[i1]);
  } INF0(")\n");
  INF3("[........] Image1: (%u,%u): %lu\n",
    bmp.xl,bmp.yl,bmp.xl*bmp.yl);
  INF3("[........] Image2: (%u,%u): %lu\n",
    bmp.xm,bmp.ym,bmp.xm*bmp.ym);
  i2=yd*xd*64ul;
  for (i1=0;i1<CNUM;++i1)
  { if (!(jpg->pp[i1]=pp[i1]=MemGet(i2)))
      ERR3("Error: Image: Nomem: PP%u[%u]",
        0ul,i1+1,i2);
  }
  for (i1=0;i1<CNUM;++i1)
  { xl=jpg->xd[i1];
    yl=jpg->yd[i1];
    xl=bmp.xm*xl/xd; xm[i1]=xl;
    yl=bmp.ym*yl/yd; ym[i1]=yl;
    INF4("[........] Comp%u: (%u,%u): %lu\n",
      i1+1,xl,yl,((N4)xl)*yl);
    if (!(jpg->buf1[i1]=buf[i1]=MemGet(4ul*yl*xl)))
      ERR4("Error: Image: Nomem: BUF1%u[%u*%u]",
        0ul,i1+1,xl,yl);
    if (!(jpg->buf[i1]=buf2[i1]=MemGet(4ul*yl*xl)))
      ERR4("Error: Image: Nomem: BUF2%u[%u*%u]",
        0ul,i1+1,xl,yl);
    if (!(jpg->buf2[i1]=buf3[i1]=MemGet(4ul*yl*xl)))
      ERR4("Error: Image: Nomem: BUF3%u[%u*%u]",
        0ul,i1+1,xl,yl);
  }
  xd2=xd*8; yd2=yd*8;

  // Read image
  for (y=0;y<bmp.ym;y+=yd2)
  { for (x=0;x<bmp.xm;x+=xd2)
    { // Read MCU
      s1=pp[0]; s2=pp[1]; s3=pp[2];
      for (i1=y,y1=0;y1<yd;++y1,i1+=8)
      { for (i2=x,x1=0;x1<xd;++x1,i2+=8)
        { for (y2=0;y2<8;++y2)
          { for (x2=0;x2<8;++x2)
            { BmpGet(&bmp,i2+x2,i1+y2,
                s1,s2,s3);
              Color1(s1++,s2++,s3++);
            }
          }
        }
      }
      // MCU downsample
      for (i1=0;i1<CNUM;++i1)
      { xl=jpg->xd[i1]; yl=jpg->yd[i1];
        if (xl!=xd)
          for (y1=0;y1<yd2;++y1)
        { for (x1=0;x1<xd2;++x1)
          { x2=x1*xl/xd;
            s1=ddyx(pp[i1],xd,y1/8,x1/8);
            s2=ddyx(pp[i1],xd,y1/8,x2/8);
            s2[y1%8*8+x2%8]=s1[y1%8*8+x1%8];
          }
        }
        if (yl!=yd)
          for (y1=0;y1<yd2;++y1)
        { y2=y1*yl/yd;
          for (x1=0;x1<xl*8;++x1)
          { s1=ddyx(pp[i1],xd,y1/8,x1/8);
            s2=ddyx(pp[i1],xd,y2/8,x1/8);
            s2[y2%8*8+x1%8]=s1[y1%8*8+x1%8];
          }
        }
      }
      // Write MCU
      for (i1=0;i1<CNUM;++i1)
      { xl=jpg->xd[i1]; yl=jpg->yd[i1];
        y1=y/yd2*yl*8; x1=x/xd2*xl*8;
        b1=buf[i1]+y1*xm[i1]+x1;
        for (y1=0;y1<yl;++y1)
        { for (x1=0;x1<xl;++x1)
          { s1=ddyx(pp[i1],xd,y1,x1);
            b2=b1+y1*8*xm[i1]+x1*8;
            for (y2=0;y2<8;++y2)
            { for (x2=0;x2<8;++x2)
                *(b2++)=*(s1++);
              b2+=xm[i1]-8;
            }
          }
        }
      }
    }
  }
  BmpRead2(&bmp);

  // DCT image
  for (i1=0;i1<CNUM;++i1)
  { b1=buf[i1];
    for (y=0;y<ym[i1];y+=8)
    { for (x=0;x<xm[i1];x+=8)
      { I2 *q1=((i1==0)?jpg->tab1:jpg->tab2);
        // Read DU
        s2=s1=pp[0];
        for (y1=0;y1<8;++y1)
        { for (x1=0;x1<8;++x1)
            *(s2++)=(N1)*(b1++);
          b1+=xm[i1]-8;
        }
        // LevelShift1,DCT1
        if (LevelShift1(s1,rr1)==-1l) return (-1l);
        if (DCT1(tt,rr1,rr2)==-1l) return (-1l);
        // ZigZag1,Quant1
        if (ZigZag(zz1,rr2,rr1)==-1l) return (-1l);
        if (Quant1(q1,rr1,dd)==-1l) return (-1l);
        // Write DU
        p1=dd; b1-=8*xm[i1];
        for (y1=0;y1<8;++y1)
        { for (x1=0;x1<8;++x1)
            *(b1++)=(N2)*(p1++);
          b1+=xm[i1]-8;
        }
        b1-=8*xm[i1]-8;
      }
      b1+=7*xm[i1];
    }
  }

  // Compress buffers
  for (i1=0;i1<CNUM;++i1)
  { tHuf h; h.i1=0; h.n1=0; h.d1=0;
    h.b1=buf[i1]; h.b2=buf2[i1];
    xl=jpg->xd[i1]; yl=jpg->yd[i1];
    for (y1=0;y1<ym[i1];y1+=yl*8)
    { for (x1=0;x1<xm[i1];x1+=xl*8)
      { for (y2=0;y2<yl;++y2)
        { for (x2=0;x2<xl;++x2)
          { for (y=0;y<8;++y)
            { for (x=0;x<8;++x)
                if (Comp1(&h)==-1l) return (-1l);
              h.b1+=xm[i1]-8;
            }
            h.b1-=8*xm[i1]-8;
          }
          h.b1+=8*xm[i1]-xl*8;
        }
        h.b1-=yl*8*xm[i1]-xl*8;
      }
      h.b1+=yl*8*xm[i1]-xm[i1];
    }
    if (h.n1) *(h.b2++)=0;
    len[i1]=h.b2-buf2[i1];
    INF3("[........] BUF%u: %lu/%lu\n",
      i1+1,h.b2-buf2[i1],h.b1-buf[i1]);
  }

  // Prepare huffman
  for (i1=0;i1<HMAX;++i1)
  { for (i2=0;i2<257;++i2)
    { itm[i1][i2].num=0;
      itm[i1][i2].len=0;
      itm[i1][i2].next=NULL;
    }
    itm[i1][256].num=1;
    for (i2=0;i2<32;++i2)
      huflen[i1][i2]=0;
  }
  // Count item number
  for (i1=0;i1<CNUM;++i1)
  { N1 n1=((i1==0)?0:2);
    b1=buf2[i1];
    for (i2=0;i2<len[i1];++i2)
    { N1 ac=((*b1&FLGDC)?0:1);
      N1 v1=(N1)(*(b1++)>>16);
      ++itm[n1+ac][v1].num;
    }
  }
  for (i1=0;i1<HMAX;++i1)
  { DEB1("[........] HufItem%u:\n",i1);
    for (i3=i2=0;i2<257;++i2)
    { N4 n1=itm[i1][i2].num;
      N2 v1=itm[i1][i2].len;
      if (n1)
      { if (i3%12==0) DEB0("[........]");
        DEB3("  %u:%lu:%u",i2,n1,v1);
        if (i3%12==11) DEB0("\n"); ++i3;
      }
    }
    if (i3%12!=0) DEB0("\n");
    // Count item length
    if (PrepItem(itm[i1])==-1l) return (-1l);
    DEB1("[........] HufItem%u:\n",i1);
    for (i3=i2=0;i2<257;++i2)
    { N4 n1=itm[i1][i2].num;
      N2 v1=itm[i1][i2].len;
      if (v1)
      { if (i3%12==0) DEB0("[........]");
        DEB3("  %u:%lu:%u",i2,n1,v1);
        if (i3%12==11) DEB0("\n"); ++i3;
      }
    }
    if (i3%12!=0) DEB0("\n");
    // Fill HufLen
    if (PrepLen1(itm[i1],huflen[i1])==-1l) return (-1l);
    DEB1("[........] HufLen%u:\n",i1);
    for (i2=0;i2<32;++i2)
    { if (i2==0) DEB0("[........]");
      DEB1("  %u",huflen[i1][i2]);
    } DEB0("\n");
    // Adjust HufLen
    if (PrepLen2(itm[i1],huflen[i1])==-1l) return (-1l);
    DEB1("[........] HufLen%u:\n",i1);
    for (i2=0;i2<32;++i2)
    { if (i2==0) DEB0("[........]");
      DEB1("  %u",huflen[i1][i2]);
    } DEB0("\n");
    // Fill HufVal
    for (i3=i2=0;i2<16;++i2)
      i3+=huflen[i1][i2];
    DEB2("[........] HufNum%u: %lu\n",i1,i3);
    if (!(jpg->htab[i1].val=hufval[i1]=MemGet(i3)))
      ERR3("Error: Huffman: Nomem: HufVal%u[%lu]",
      0ul,i1,i3);
    if (PrepVal(itm[i1],hufval[i1],(N2)i3)==-1l) return (-1l);
    // Fill htab[]
    jpg->htab[i1].hid=(N1)(i1>>1);
    jpg->htab[i1].typ=(N1)(i1&1);
    jpg->htab[i1].hufnum=(N2)i3;
    jpg->htab[i1].val=hufval[i1];
    for (i2=0;i2<16;++i2)
      jpg->htab[i1].len[i2]=huflen[i1][i2];
    if (!(jpg->htab[i1].hufsiz=MemGet(1*i3)))
      ERR3("Error: Huffman: Nomem: HufSiz%u[%lu]",
      0ul,i1,i3);
    if (!(jpg->htab[i1].hufcod=MemGet(2*i3)))
      ERR3("Error: Huffman: Nomem: HufCod%u[%lu]",
      0ul,i1,i3);
    if (PrepTab(jpg->htab+i1,0)==-1l) return (-1l);
  }

  // Fill Jpeg info
  jpg->xd0=xd; jpg->yd0=yd;
  jpg->xl=bmp.xl; jpg->yl=bmp.yl;
  jpg->xm=bmp.xm; jpg->ym=bmp.ym;
  for (i1=0;i1<CNUM;++i1)
  { jpg->buf[i1]=buf2[i1];
    jpg->len[i1]=len[i1];
  }

  // Decompress buffers
  /*for (i1=0;i1<CNUM;++i1)
  { tHuf h; h.i1=0; h.n1=0; h.d1=0;
    h.b1=buf3[i1]; h.b2=buf2[i1];
    h.v1=0; i3=((N4)ym[i1])*xm[i1];
    xl=jpg->xd[i1]; yl=jpg->yd[i1];
    for (y1=0;y1<ym[i1];y1+=yl*8)
    { for (x1=0;x1<xm[i1];x1+=xl*8)
      { for (y2=0;y2<yl;++y2)
        { for (x2=0;x2<xl;++x2)
          { for (y=0;y<8;++y)
            { for (x=0;x<8;++x)
                if (Comp2(&h,i3)==-1l) return (-1l);
              h.b1+=xm[i1]-8;
            }
            h.b1-=8*xm[i1]-8;
          }
          h.b1+=8*xm[i1]-xl*8;
        }
        h.b1-=yl*8*xm[i1]-xl*8;
      }
      h.b1+=yl*8*xm[i1]-xm[i1];
    }
    INF5("[........] BUF%u: %lu,%lu/%lu:%lu\n",
      i1+1,h.i1,h.b1-buf3[i1],i3,h.b2-buf2[i1]);
  }*/

  // Test buffers
  /*for (i1=0;i1<CNUM;++i1)
  { i3=((N4)ym[i1])*xm[i1];
    b1=buf[i1]; b2=buf3[i1];
    for (i2=0;i2<i3;++i2)
      if (*(b1++)&0xFFFF0000ul) break;
    if (i2!=i3)
      INF3("[........] HIBUF1%u: %lu/%lu\n",
      i1+1,i2,i3);
    for (i2=0;i2<i3;++i2)
      if (*(b2++)&0xFFFF0000ul) break;
    if (i2!=i3)
      INF3("[........] HIBUF2%u: %lu/%lu\n",
      i1+1,i2,i3);
    b1=buf[i1]; b2=buf3[i1];
    for (i2=0;i2<i3;++i2)
      if (*(b1++)!=*(b2++)) break;
    if (i2!=i3)
      INF3("[........] DIFF%u: %lu/%lu\n",
      i1+1,i2,i3);
  }*/

  // iDCT image
  /*for (i1=0;i1<CNUM;++i1)
  { b1=buf3[i1];
    for (y=0;y<ym[i1];y+=8)
    { for (x=0;x<xm[i1];x+=8)
      { I2 *q1=((i1==0)?jpg->tab1:jpg->tab2);
        // Read DU
        p1=dd; s1=pp[0];
        for (y1=0;y1<8;++y1)
        { for (x1=0;x1<8;++x1)
            *(p1++)=(I2)*(b1++);
          b1+=xm[i1]-8;
        }
        // Quant2,ZigZag2
        if (Quant2(q1,dd,rr1)==-1l) return (-1l);
        if (ZigZag(zz2,rr1,rr2)==-1l) return (-1l);
        // DCT2,LevelShift2
        if (DCT2(tt,rr2,rr1)==-1l) return (-1l);
        if (LevelShift2(rr1,s1)==-1l) return (-1l);
        // Write DU
        b1-=8*xm[i1];
        for (y1=0;y1<8;++y1)
        { for (x1=0;x1<8;++x1)
            *(b1++)=*(s1++);
          b1+=xm[i1]-8;
        }
        b1-=8*xm[i1]-8;
      }
      b1+=7*xm[i1];
    }
  }*/
  
  // Write image
  /*i1=0; b1=buf3[i1];
  BmpWrite1(&bmp,prg->fb,
    xm[i1],ym[i1],xm[i1],ym[i1]);
  for (y=0;y<ym[i1];++y)
    for (x=0;x<xm[i1];++x,++b1)
      BmpSet(&bmp,x,y,
        (N1)*b1,(N1)*b1,(N1)*b1);
  BmpWrite2(&bmp);*/

  return (0);
}

I4 conv1b(tJpeg1 *jpg,FILE *f1)
{ tPutBit b1; tHtab *h1;
  N2 x,y; N1 xd2,yd2;
  N4 i1,i2,i3=0,nn=0;
  N4 *p1[CNUM],n1[CNUM];
  b1.nn=&nn; b1.f1=f1;
  b1.cnt=0; b1.bb=0;

  xd2=jpg->xd0*8;
  yd2=jpg->yd0*8;
  for (i1=0;i1<CNUM;++i1)
  { p1[i1]=jpg->buf[i1];
    n1[i1]=jpg->xd[i1]*jpg->yd[i1];
  }
  for (y=0;y<jpg->ym;y+=yd2)
  { for (x=0;x<jpg->xm;x+=xd2)
    { if (i3%8==0) DEB0("[........] "); DEB0("<");
      for (i1=0;i1<CNUM;++i1)
      { if (i1!=0) DEB0(":");
        for (i2=0;i2<n1[i1];++i2)
        { DEB0(".");
          if (!(*p1[i1]&FLGDC))
            ERR1("Error: conv2: DC",0ul);
          h1=jpg->htab+((i1==0)?0:2);
          if (Encode(&b1,h1+0,*(p1[i1]++))==-1l) return (-1l);
          while (p1[i1]-jpg->buf[i1]<jpg->len[i1] && !(*p1[i1]&FLGDC))
            if (Encode(&b1,h1+1,*(p1[i1]++))==-1l) return (-1l);
        }
      }
      DEB0(">"); ++i3;
      if (!(i3%8)) DEB0("\n");
    }
  }
  if (i3%8) DEB0("\n");
  if (b1.cnt && PutVal(&b1,0xFFu,8-b1.cnt)==-1l)
    return (-1l);

  return (0);
}


#endif
